window.VUEADF = window.VUEADF || {};
window.VUEADF.control = window.VUEADF.control || {};
window.VUEADF.domControl = window.VUEADF.domControl || {};
window.VUEADF.model = window.VUEADF.model || {};
window.VUEADF.policy = window.VUEADF.policy || {};
window.VUEADF.editpart = window.VUEADF.editpart || {};
window.VUEADF.figure = window.VUEADF.figure || {};
window.VUEADF.action = window.VUEADF.action || {};
window.VUEADF.actionModel = window.VUEADF.actionModel || {};


window.VUEADF.tool = window.VUEADF.tool || {};
window.VUEADF.property = window.VUEADF.property || {};
window.VUEADF.propertyDescriptor = window.VUEADF.propertyDescriptor || {};


window.VUEADF.EditPolicy = window.VUEADF.EditPolicy || {};
/**
 * Created by xiongshilin on 2017/3/24.
 */
(function(){
    VUEADF.Control = function(parent) {
        VUEADF.Control.superclass.constructor.call(this, parent);
        this.vueData = {};
    };
    COM.util.defineClass(VUEADF.Control, AGEF.Control, {
        getBounds:function(){
            // var bounds = COM.util.HTMLUtil.getBounds(this.domElement);
            // var parentControlElement = this.getParent().getDomElement();
            // var parentDomElement = this.domElement.parentElement;
            // while(parentControlElement !== parentDomElement){
            //     var parentBounds = COM.util.HTMLUtil.getBounds(parentDomElement);
            //     bounds.x = bounds.x + parentBounds.x + 1;//(父容器内边框的高度) + 1（父容器边框）
            //     bounds.y = bounds.y + parentBounds.y + 1;//要坐标高度20（标题栏高度） + 1（父容器边框）
            //     parentDomElement = parentDomElement.parentElement;
            // }
            // return bounds;
            return {
                x : 100,
                y : 100,
                width : 100,
                height : 100
            }
        },

        createDomElement: function(){},


        getVueData : function(){
            return this.vueData;
        },

        getVM : function(){
            return null;
        }

    });
})();
/**
 * Created by xiongshilin on 2017/3/24.
 */
(function(){
    VUEADF.Composite = function(parent) {
        VUEADF.Composite.superclass.constructor.call(this, parent);
        this.children = [];
        this.vueData = {
            name : "div",
            data : {
                children : []
            }
        }
    };
    COM.util.defineClass(VUEADF.Composite, VUEADF.Control, {
        getChildren: function(){
            return this.children;
        },
        addChild: function(child, index) {
            if(arguments.length == 2) {
                this.children.splice(index, 0, child);
            } else {
                this.children.push(child);
            }
            child.setParent(this);
            // this.appendDomChild(child.getDomElement());
            this.appendChildData(child.getVueData());
        },
        appendChildData : function(childData){
            this.vueData.data.children.push(childData);
        },
        removeChildData : function(childData){
            COM.util.Array.removeItem(this.vueData.data.children, childData);
        },
        appendDomChild: Function.needOverride,
        removeChild: function(child){
            COM.util.Array.removeItem(this.children, child);
            this.removeChildData(child.getVueData());
            // this.removeDomChild(child.getDomElement());
        },
        layoutChild: Function.emptyFunction,
        _childLayoutChanged : function(){
            return false;
        },
        removeDomChild: Function.needOverride,
        getChild: function(index) {
            if(0 < index && index < this.children.length) {
                return this.children[index];
            }
            return null;
        },
        clearChildren:function() {
            this.children = [];
        }
    });
})();
(function(){

    VUEADF.domControl.XYComposite = function(parent) {
        VUEADF.domControl.XYComposite.superclass.constructor.call(this, parent);
        this.vueData = {
            name : "XYComposite",
            data : {
                children : [],
                style: {}
            }
        }
    };

    COM.util.defineClass(VUEADF.domControl.XYComposite, VUEADF.Composite, {
        getLabel: function(){
            return "XYComposite";
        },
        // appendDomChild: function(child) {
        //     this.getDomElement().appendChild(child);
        // },
        // removeDomChild: function(child) {
        //     this.getDomElement().removeChild(child);
        // },
        // _getDomElement: function(model) {
        //     var composite = COM.util.HTMLUtil.createELement('<div class="VUEADF_xy_composite"></div>');
        //     this.domElement = composite[0];
        //     if(model.isGrid) {
        //         this.createGridLine(this.domElement);
        //     }
        //     return this.domElement;
        // },
        // createGridLine: function(dom) {
        //     var svg = COM.util.HTMLUtil.createELement('<svg xmlns="http://www.w3.org/2000/svg" id="main" version="1.1" style="position:absolute" width="100%" height="100%"></svg>');
        //     for(var i=1; i< 200; i++) {
        //         var x = i*15 - 0.5;
        //         var hline = COM.util.HTMLUtil.createELement('<line x1='+x+' y1="0" x2='+x+' y2="100%" style="stroke:rgba(120,120,120,0.3);stroke-width:1;stroke-dasharray:1;"/>');
        //         var vline = COM.util.HTMLUtil.createELement('<line x1="0" y1='+x+' x2="100%" y2='+x+' style="stroke:rgba(120,120,120,0.3);stroke-width:1;stroke-dasharray:1;"/>');
        //         svg.append(hline);
        //         svg.append(vline);
        //     }
        //     svg.html(svg.html());
        //     dom.appendChild(svg[0]);
        // },
        layoutChild : function(child){
            var childDom = child.getDomElement();
            var layoutData = child.getLayoutData();
            if(layoutData){
                var css = {"display":"block", "position":"absolute", "left":layoutData.x + 'px', "top":layoutData.y + 'px'};
                if(layoutData.width >= 0) {
                    css.width = layoutData.width + 'px';
                }
                if(layoutData.height >= 0) {
                    css.height = layoutData.height + 'px';
                }
                $(childDom).css(css);
            }
            child._afterLayout();
        }
    });
})();
VUEADF.policy.XYCompositeLayoutPolicy = function(){
    VUEADF.policy.XYCompositeLayoutPolicy.superclass.constructor.call(this);
};

COM.util.defineClass(VUEADF.policy.XYCompositeLayoutPolicy, COM.gef.XYLayoutEditPolicy, {
    createChangeConstraintCommand: function(request, child, rect){
      var model = child.getModel(),
          parentFigure = child.parent.getFigure().getBounds();
      if(rect.width < 30 || rect.height < 30) {
        return COM.gef.Command.UNEXECUTABLE_COMMAND;
      }

      return COM.gef.Command.createCommand({
        label: 'change bounds',
        execute: function(){
          this.old = model.getLayoutData();
          var boundWidth = parentFigure.width - rect.width,
              boundHeight = parentFigure.height - rect.height;
          if((rect.x > 0 &&rect.x < boundWidth) && (rect.y > 0 &&rect.y < boundHeight)){
              model.setLayoutData(rect);
          }else{
              model.setLayoutData(this.old);
          }
        },
        undo: function(){
          model.setLayoutData(this.old);
        }
      });

    },
    getCreateCommand: function(request){
      return COM.gef.Command.createCommand({
        label: 'create child',
        child: request.getNewObject(),
        parent: this.getHost().getModel(),
        figure: this.getHost().getFigure(),
        execute: function(){
          var models;
          if(this.child instanceof VUEADF.model.CompoundModel){
            models = this.child.getModels();
          }else{
            models = [this.child];
          }
          var copy = this.parent.getLayoutData().copy();
          this.figure.translateToAbsolute(copy);
          var relativeLoaction = new COM.draw2d.Point(request.location.x - copy.x, request.location.y - copy.y);
          var rel;
          for (var i = 0; i < models.length; i++) {
            var model = models[i];
            if(i > 0){
              relativeLoaction.x = relativeLoaction.x + models[i - 1].getLayoutData().width - 1;
            }
            this.parent.addChild(model);
            var bounds = new COM.draw2d.Rectangle();
            bounds.width = 63;
            bounds.height = 33;
            bounds.setLocation(relativeLoaction);
            model.setLayoutData(bounds);
          }
        },
        undo: function(){
          var models;
          if(this.child instanceof VUEADF.model.CompoundModel){
            models = this.child.getModels();
          }else{
            models = [this.child];
          }
          for (var i = 0; i < models.length; i++) {
            var model = models[i];
            this.parent.removeChild(model);
          }
        }
      });
    },
    createAddCommand: function(request, child, rect) {
      var command = new COM.gef.CompoundCommand(), deleteCommand = null, addCommand = null;
      deleteCommand = COM.gef.Command.createCommand({
        label: 'delete model',
        parentModel: child.getParent().getModel(),
        childModel:child.getModel(),
        oldBounds: child.getModel().getLayoutData(),
        execute: function(){
          this.parentModel.removeChild(this.childModel);
        },
        undo: function(){
          this.parentModel.addChild(this.childModel);
          this.childModel.setLayoutData(this.oldBounds);
        }
      });
      addCommand = COM.gef.Command.createCommand({
        label: 'add model',
        parentModel: this.getHost().getModel(),
        childModel:child.getModel(),
        execute: function(){
          this.parentModel.addChild(this.childModel);
          this.childModel.setLayoutData(rect);
        },
        undo: function(){
          this.parentModel.removeChild(this.childModel);
        }
      });
      command.add(deleteCommand);
      command.add(addCommand);
      return command.unwrap();
    },
    getConstraintFor: function(request, child) {
      var rectangle = new COM.draw2d.Rectangle(), childBoundsCopy = child.getModel().getLayoutData().copy(),
          hostModel = this.getHost().getModel(), copy = hostModel.getLayoutData().copy(),
          hostFigure = this.getHost().getFigure(), childFigure = child.getFigure(), moveData = request.getMoveDelta();
      hostFigure.translateToAbsolute(copy);
      childFigure.translateToAbsolute(childBoundsCopy);
      var relativeLoaction = new COM.draw2d.Point(childBoundsCopy.x + moveData.x - copy.x  , childBoundsCopy.y + moveData.y - copy.y);
      rectangle.setLocation(relativeLoaction);
      rectangle.setSize(child.getFigure().getLayoutData().getSize());
      return rectangle;
    }
});(function(){
    VUEADF.figure.XYCompositeFigure = function() {
        VUEADF.figure.XYCompositeFigure.superclass.constructor.call(this);
        this.setLayoutManager(new COM.draw2d.XYLayout());
    };

    COM.util.defineClass(VUEADF.figure.XYCompositeFigure, AGEF.CompositeFigure, {
        paintChildren: function(graphics){
            VUEADF.figure.XYCompositeFigure.superclass.paintChildren.call(this, graphics);
        },
        useLocalCoordinates: function() {
            return true;
        }
    });
})();
VUEADF.model.XYCompositeModel = function(isGrid) {
    VUEADF.model.XYCompositeModel.superclass.constructor.call(this);
    this.isGrid = isGrid;
};

COM.util.defineClass(VUEADF.model.XYCompositeModel, ADF.model.CompositeModel, {
    _getModelType: function(){
        return "VUEADF.model.XYCompositeModel";
    },
    _getName : function(){
        return 'xyComposite';  
    },
    _getShowText : function(){
        return "XYComposite";
    }
});/**
 * @author zhengjiachao
 * @since 2017-3-27
 */
VUEADF.control.XYComposite = function() {
    VUEADF.control.XYComposite.superclass.constructor.call(this);
};

COM.util.defineClass(VUEADF.control.XYComposite, ADF.CompositeDescriptor, {

    getEditpart: function() {
        var editpart = function() {
            editpart.superclass.constructor.call(this);
        };
        COM.util.defineClass(editpart, ADF.editpart.CompositeEditpart, {
            _createFigure: function() {
                var xyCompositeFigure = new VUEADF.figure.XYCompositeFigure();
                var xyComposite = this.getWidgetFactory().createControl(this.getModel());
                xyCompositeFigure.setWidget(xyComposite);
                return xyCompositeFigure;
            },
            createEditPolicies: function() {
                editpart.superclass.createEditPolicies.call(this);
                this.installEditPolicy(COM.gef.EditPolicy.LAYOUT_ROLE, new VUEADF.policy.XYCompositeLayoutPolicy());
                // this.installEditPolicy(COM.gef.EditPolicy.SELECTION_FEEDBACK_ROLE, new VUEADF.policy.SnapPolicy());
            },
            getModelChildren: function(){
                var composite = this.getModel();
                return composite.getChildren();
            },
            refreshChildren: AGEF.functions.editPartRefreshChildren,
        });
        return editpart;
    },

    getModel: function() {
        return VUEADF.model.XYCompositeModel;
    },

    getDomControl : function(){
        return VUEADF.domControl.XYComposite;
    }

});

/**
 * Created by xiongshilin on 2017/3/29.
 */
(function(){
    VUEADF.ContentsControl = function(){
        VUEADF.ContentsControl.superclass.constructor.call(this, null);
    };
    COM.util.defineClass(VUEADF.ContentsControl, VUEADF.Composite, {
        getLabel: function() {
            return "DefaultContentsControl";
        },
        createDomElement: function() {
            var element = document.createElement("div");
            element.id = "contentscontrol";
            element.style.position="relative";
            element.style.top="0px";
            element.style.left="0px";
            element.style.width="100%";
            element.style.height="100%";
            this.domElement = element;
        },
        getVM : function(){
            return diagramVM;
        },
        appendChildData : function(childData){
            this.getVM().children.push(childData);
        },
        setWidth:function(width) {
            if(this.domElement) {
                this.domElement.style.width = width + "px";
            }
        },
        getWidth: function() {
            if(this.domElement) {
                return COM.util.HTMLUtil.getElementWidth(this.domElement);
            }
        },
        setHeight:function(height) {
            if(this.domElement) {
                this.domElement.style.height = height + "px";
            }
        },
        getHeight: function() {
            if(this.domElement) {
                return COM.util.HTMLUtil.getElementHeight(this.domElement);
            }
        },
        setSize: function(width, height) {
            if(this.domElement) {
                this.domElement.style.width = width + "px";
                this.domElement.style.height = height + "px";
            }
        },
        appendDomChild: function(domChild) {
            // COM.util.HTMLUtil.addChild(this.domElement, domChild);
        },
        removeDomChild: function(domChild) {
            // COM.util.HTMLUtil.removeChild(domChild);
        },
        getDomElement: function(){
            if(!this.domElement) {
                this.createDomElement();
            }
            return this.domElement;
        },
        layoutChild : function(child){
            var childDom = child.getDomElement();
            var layoutData = child.getLayoutData();
            if(layoutData){
                var css = {"display":"block", "position":"absolute", "left":layoutData.x + 'px', "top":layoutData.y + 'px'};
                if(layoutData.width >= 0) {
                    css.width = layoutData.width + 'px';
                }
                if(layoutData.height >= 0) {
                    css.height = layoutData.height + 'px';
                }
                $(childDom).css(css);
            }
            if(child._afterLayout){
                child._afterLayout();
            }
        }
    });
})();/**
 * @author zhengjiachao
 * @since 2017-3-27
 */

VUEADF.model.DiagramModel = function(){
    VUEADF.model.DiagramModel.superclass.constructor.call(this);
}

VUEADF.model.DiagramModel.PROPERTY_CHILDREN = "children";

COM.util.defineClass(VUEADF.model.DiagramModel, ADF.model.DiagramModel,{

});

/**
 * Created by xiongshilin on 2017/3/29.
 */
(function(){
    VUEADF.figure.DiagramFigure = function() {
        VUEADF.figure.DiagramFigure.superclass.constructor.call(this);
    };
    COM.util.defineClass(VUEADF.figure.DiagramFigure, ADF.figure.DiagramFigure, {
        getWidget:function() {
            if(!this.widget) {
                this.widget = new VUEADF.ContentsControl();
            }
            return this.widget;
        }
    });
})();VUEADF.editpart.DiagramEditPart = function(){
  VUEADF.editpart.DiagramEditPart.superclass.constructor.call(this);
}

COM.util.defineClass(VUEADF.editpart.DiagramEditPart, ADF.editpart.DiagramEditPart,{

    createFigure: function(){
        var diagramFigure = new VUEADF.figure.DiagramFigure();
        return diagramFigure;
    }
});
/**
 * @author zhengjiachao
 * @since 2017-3-27
 */
VUEADF.control.Diagram = function(){
    VUEADF.control.Diagram.superclass.constructor.call(this);
};

COM.util.defineClass(VUEADF.control.Diagram, ADF.control.Diagram,{

    getEditpart : function(){
        return VUEADF.editpart.DiagramEditPart;
    },

    getModel : function(){
        return VUEADF.model.DiagramModel;
    }
    

});